﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace KeepPwdPcInput
{
    class ConfigHelper
    {
        [Serializable]
        public class config
        {
            public string connType { set; get; }
            public string connKey { set; get; }
            public int port { set; get; }
        }
        static string configkey = "hufjf#uweyds%kushdfg$u1564dsfk";

        public static void save(String connType, String connKey,int port)
        {
            config co = new config();
            co.connKey = connKey;
            co.connType = connType;
            co.port = port;
            JavaScriptSerializer js = new JavaScriptSerializer();
            String ss = js.Serialize(co);
            ss = AES.Encrypt(ss, configkey);
            using (FileStream fs = new FileStream("config.data",FileMode.Create,FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(ss);
                }
            }
        }

        public static config getCfg()
        {
            try
            {
                String ss = "";
                using (FileStream fs = new FileStream("config.data", FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        ; ss = sr.ReadToEnd();
                    }
                }
                ss = AES.Decrypt(ss, configkey);
                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Deserialize<config>(ss);
            }
            catch
            {
                return null;
            }
        }
    }
}
